package net.ys.controller;

import net.ys.bean.AprApollo;
import net.ys.service.ApiService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: NMY
 * @Date: 2022/3/15
 * @Time: 11:03
 */
@Controller
@RequestMapping("/web")
public class WebController {

    @Resource
    private ApiService apiService;

    @GetMapping("/apollo/list")
    public String listApollo(Model model, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int pageSize) {
        if (page < 1) {
            page = 1;
        }
        long count = apiService.queryApolloCount();

        long t = count / pageSize;
        int k = count % pageSize == 0 ? 0 : 1;
        int totalPage = (int) (t + k);

        if (page > totalPage && count > 0) {
            page = totalPage;
        }

        List<AprApollo> aprApollos;
        if ((page - 1) * pageSize < count) {
            aprApollos = apiService.listApollo(page, pageSize);
        } else {
            aprApollos = new ArrayList<>();
        }

        model.addAttribute("count", count);
        model.addAttribute("currPage", page);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("apollos", aprApollos);
        return "apollo/list";
    }
}
